//
//  UIImage+UIImage_Tool.m
//  UpYunSDKDemo
//
//  Created by 方伟 on 16/5/25.
//  Copyright © 2016年 upyun. All rights reserved.
//

#import "UIImage+UIImage_Tool.h"

@implementation UIImage (UIImage_Tool)



- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)targetSize{
    
    CGFloat scaleFactor = 0.0;
    CGSize newSize = img.size;

    // 绘制改变大小的图片
    
    if (CGSizeEqualToSize(img.size, targetSize) == NO)
    {
        scaleFactor = targetSize.width / img.size.width;
//        CGFloat heightFactor = targetSize.height / img.size.height;
        
//        if (widthFactor > heightFactor)
//            scaleFactor = widthFactor; // scale to fit height
//        else{
//            scaleFactor = heightFactor;// scale to fit width
//        }
         newSize.width  = img.size.width * scaleFactor;
         newSize.height = img.size.height * scaleFactor;
    }
    
    
    // 创建一个bitmap的context

    // 并把它设置成为当前正在使用的context

    UIGraphicsBeginImageContext(newSize);

    
    [img drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    NSLog(@"%f",scaledImage.size.height);
    return scaledImage;
    
}


@end
